package com.web;

import com.javabean.Book;
import com.javabean.Page;
import com.service.impl.BookServiceImpl;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        BookServiceImpl a = new BookServiceImpl();
        a.addBook(book);
        //请求重定向
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=paging&pageNum=" + request.getParameter("pageNum"));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer id = Integer.valueOf(request.getParameter("id"));
        BookServiceImpl a = new BookServiceImpl();
        a.deleteBookById(id);
        //请求重定向
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=paging");
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Book book = WebUtils.copyParamToBean(request.getParameterMap(), new Book());
        BookServiceImpl a = new BookServiceImpl();
        a.updateBook(book);
        //请求重定向
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=paging&pageNum=" + request.getParameter("pageNum"));

    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询全部图书
        BookServiceImpl a = new BookServiceImpl();
        List<Book> books = a.queryBooks();
        //把结果放在request域中
        request.setAttribute("books", books);
        //请求转发
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    //获得要修改的图书的信息
    public void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String pageNum = request.getParameter("pageNum");
        int i = Integer.parseInt(pageNum);
        BookServiceImpl a = new BookServiceImpl();
        Book book = a.queryBookById(id);
        request.setAttribute("pageNum", i);
        request.setAttribute("book", book);
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }


    /**
     * 处理分页
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void paging(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数pageNum,pageSize
        String pageNum = request.getParameter("pageNum");
        if (pageNum == null) {
            //2.调用BookService.page(pageNum,pageSize)
            BookServiceImpl a = new BookServiceImpl();
            Page<Book> page = a.page(1, 4);
            //3. 保存数据到request域中
            request.setAttribute("page", page);
            //4.请求转发
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
        } else {
            int i = Integer.parseInt(pageNum);
            //2.调用BookService.page(pageNum,pageSize)
            BookServiceImpl a = new BookServiceImpl();
            Page<Book> page = a.page(i, 4);
            //3. 保存数据到request域中
            request.setAttribute("page", page);
            //4.请求转发
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
        }
    }

    //跳转到选择页
    public void forpaging(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取输入的页号
        String pn = request.getParameter("pn");
        int i = Integer.parseInt(pn);
        BookServiceImpl a = new BookServiceImpl();
        Page<Book> page = a.page(i, 4);
        //3. 保存数据到request域中
        request.setAttribute("pn", i);
        request.setAttribute("page", page);
        Integer pageTotal = page.getPageTotal();
        if (1 <= i && i <= pageTotal) {
            //4.请求转发
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
        } else if (i > pageTotal) {
            request.setAttribute("pn", pageTotal);
            page = a.page(pageTotal, 4);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
        } else if (i < 1) {
            request.setAttribute("pn", 1);
            page = a.page(1, 4);
            request.setAttribute("page", page);
            request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
        }
    }
}
